import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    version: '1.0',
    cartShops: [],
    cartShops_Maps: {}, //map格式
    diashes: [],
  },
  mutations: {
    add_diashes(state, data) {
      //有值就++ 每值就+1
      if (data) {
        const hasIndex = state.cartShops.findIndex((x) => data.id === x.id);
        if (hasIndex !== -1) {
          const new_obj = Object.assign({}, state.cartShops[hasIndex], { num: state.cartShops[hasIndex].num + 1 });
          Vue.set(state.cartShops, hasIndex, new_obj);
        } else {
          data.num = 1;
          state.cartShops.push(data);
        }
        //删除
        Vue.delete(state.cartShops_Maps);
        state.cartShops.forEach((item) => {
          Vue.set(state.cartShops_Maps, item.id, item.num);
        });
      }
    },
    add_total_foods(state, data) {
      //有值就++ 每值就+1
      if (data) {
        state.diashes = data;
      } else {
        state.diashes = [];
      }
    },
  },
  actions: {},
  modules: {},
});
